import React,{PureComponent} from 'react';
import { Form, Input, Button, Alert, Divider, Cascader, Card, Icon, Upload, Radio,DatePicker,InputNumber,message} from 'antd';
import { routerRedux } from 'dva/router';
import { connect } from 'dva';
import constants from '../../../common/constants';
import { digitUppercase } from '../../../utils/utils';
import styles from './goodsManage.less';
import {powerControl} from '../../../utils/powerControl';
const AuthButton = powerControl(Button);
const FormItem = Form.Item;
const { TextArea } = Input;
const RadioGroup = Radio.Group;
const { RangePicker } = DatePicker;

@connect(state => ({
    submitting: state.goodsManage.loading,
}))
@Form.create()
export default class GoodsSI extends PureComponent {

    state = {
        loading: false,
        sellerShop: []
    };

    componentDidMount() {
        const {dispatch} = this.props;
        dispatch({
            type: 'goodsManage/sellerShop',
            callback: (response) => {
                if (response.result === 0) {
                    this.setState({
                        sellerShop:response.resultData.sellerShopList
                    })
                }
            }
        });
    }

    onPrev = () => {
        const {dispatch} = this.props;
        dispatch(routerRedux.push('/goods/addGoods/goodsAttr'));
    };

    onValidateForm = (e) => {
        const {dispatch, data} = this.props;
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                const userInfoState = sessionStorage.getItem("userInfoState");
                let user_id = '';
                if (userInfoState !== null) {
                    const {userInfo} = JSON.parse(userInfoState);
                    user_id = userInfo.user_id;
                }
                if (user_id != ''){
                    dispatch({
                        type: 'goodsManage/keepData',
                        payload: {
                            ...values,
                        },
                        callback: (response) => {
                            if (response){
                                const editType = data.editType;
                                this.props.submit(editType,user_id,values);
                            }
                        }
                    });
                }else {
                    message.error("登入信息异常！请重新登入")
                }
            }
        });
    }
    render() {
        const { formItemLayout, data } = this.props;
        const {getFieldDecorator} = this.props.form;
        let fromSellerId = data.editType === 'add' ? [] : eval("["+data.from_seller_id+"]");
        return(
            <Card title="其他信息" bordered={false}>
                <Form layout="horizontal" className={styles.stepForm}>
                    <FormItem
                        {...formItemLayout}
                        label={<span>赠送积分</span>}
                    >
                        {getFieldDecorator('give_integral', {
                            initialValue: data.give_integral || 0,
                            rules: [{
                                type: 'integer',
                            }],
                        })(
                            <InputNumber min={0} step={1} />
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label={<span>等级积分</span>}
                    >
                        {getFieldDecorator('rank_integral', {
                            initialValue: data.rank_integral || 0,
                            rules: [{
                                type: 'integer',
                            }],
                        })(
                            <InputNumber min={0} step={1} />
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label={<span>商品标签</span>}
                    >
                        {getFieldDecorator('goods_tag', {
                            initialValue: data.goods_tag || '',
                            rules: [{
                            }],
                        })(
                            <Input placeholder="商品标签"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label={<span>计量单位</span>}
                    >
                        {getFieldDecorator('measure_unit', {
                            initialValue: data.measure_unit || '',
                            rules: [{
                            }],
                        })(
                            <Input placeholder="计量单位"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label={<span>所属商家</span>}
                    >
                        {getFieldDecorator('from_seller_id', {
                            initialValue: fromSellerId || [],
                            rules: [{
                                type:"array",
                                required: true, message: '请选择商家',
                            }],
                        })(
                            <Cascader changeOnSelect options={this.state.sellerShop} />
                        )}
                    </FormItem>
                    <FormItem
                        label={<span>商品的商家备注</span>}
                    >
                        {getFieldDecorator('seller_note', {
                            initialValue: data.seller_note || '',
                            rules: [{
                            }],
                        })(
                            <TextArea rows={4} />
                        )}
                    </FormItem>

                    <Divider style={{ margin: '24px 0' }} />
                    <Form.Item
                        style={{ marginBottom: 8 }}
                        wrapperCol={{
                            xs: { span: 24, offset: 0 },
                            sm: { span: formItemLayout.wrapperCol.span, offset: formItemLayout.labelCol.span },
                        }}
                        label=""
                    >
                        <Button onClick={this.onPrev}>
                            上一步
                        </Button>
                        <AuthButton auth="goodsManageRelease" type="primary" onClick={this.onValidateForm} style={{ marginLeft: 8 }}>
                            发布商品
                        </AuthButton>
                    </Form.Item>
                </Form>
            </Card>
        )
    }
}

